using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using System.Xml.Serialization;

namespace GameLib
{
    class GameData
    {
        public static SaveGameData SaveData = new SaveGameData();

        public struct LevelResult
        {
            public String Level;

            public String Star;

            public String Score;
        }

        public class SaveGameData
        {
            public String Score;

            public String Level;

            public List<LevelResult> GameResult;

            public void GenerateData()
            {
                for (int i = 0; i < 10; i++)
                {
                    LevelResult lv = new LevelResult();
                    lv.Level = (i + 1).ToString();
                    lv.Star = "3";
                    lv.Score = "100";
                    GameResult.Add(lv);
                }

                Score = "1000";
                Level = "10";
            }

            public SaveGameData()
            {
                GameResult = new List<LevelResult>();
            }
        }

        public static void GenerateDefault()
        {
            SaveData.Level = "5";
            SaveData.Score = "100";
            for (int i = 0; i < 5; i++)
            {
                LevelResult lv = new LevelResult();
                lv.Level = (i + 1).ToString();
                lv.Star = "0";
                lv.Score = "100";
                SaveData.GameResult.Add(lv);
            }
        }

        public static void DeleteSave()
        {
            using (IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (savegameStorage.FileExists("savegame.sav"))
                {
                    savegameStorage.DeleteFile("savegame.sav");
                }
            }
        }

        public static void SaveGame()
        {
            SaveGameData data = new SaveGameData();
            data.GenerateData();

            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("savegame.sav", FileMode.Create))
                {
                    using (XmlWriter writer = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        writer.WriteStartDocument();
                        writer.WriteStartElement("SaveGame");
                        
                        writer.WriteElementString("Score", data.Score);
                        writer.WriteElementString("Level", data.Level);
                        
                        writer.WriteStartElement("LevelResult");
                        int lv= Convert.ToInt32(data.Level);
                        for (int i = 0; i < lv; i++)
                        {
                            writer.WriteStartElement("Result");
                            writer.WriteAttributeString("Level", data.GameResult[i].Level);
                            writer.WriteAttributeString("Score", data.GameResult[i].Score);
                            writer.WriteAttributeString("Star", data.GameResult[i].Star);
                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();
                        
                        writer.WriteEndElement();
                        writer.WriteEndDocument();
                        writer.Flush();
                    }
                }
            }
        }

        public static bool LoadGame()
        {
            using (IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (savegameStorage.FileExists("savegame.sav"))
                {
                    using (IsolatedStorageFileStream stream = savegameStorage.OpenFile("savegame.sav", System.IO.FileMode.Open))
                    {
                        using (XmlReader reader = XmlReader.Create(stream))
                        {
                            SaveGameData data = new SaveGameData();

                            reader.Read();
                            reader.ReadStartElement("SaveGame");

                            reader.ReadStartElement("Score");
                            data.Score = reader.ReadContentAsString();
                            reader.ReadEndElement();
                            
                            reader.ReadStartElement("Level");
                            data.Level = reader.ReadContentAsString();
                            reader.ReadEndElement();

                            reader.ReadStartElement("LevelResult");
                            int lv = Convert.ToInt32(data.Level);
                            for (int i = 0; i < lv; i++)
                            {
                                do
                                {
                                    reader.Read();
                                } while (!reader.IsStartElement());
                                int n = reader.AttributeCount;
                                LevelResult lvRes = new LevelResult();
                                lvRes.Level = reader.GetAttribute(0);
                                lvRes.Score = reader.GetAttribute(1);
                                lvRes.Star = reader.GetAttribute(2);
                                data.GameResult.Add(lvRes);
                            }
                            reader.Read();

                            reader.ReadEndElement();
                            reader.ReadEndElement();

                            SaveData = data;
                            return true;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
